package com.life.wd.service.Imp;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.life.wd.dto.VideoDTO;
import com.life.wd.dto.VideoSearchDTO;
import com.life.wd.mapper.VideoMapper;
import com.life.wd.result.PageResult;
import com.life.wd.service.VideoService;
import com.life.wd.vo.VideoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.util.List;

@Service
public class VideoServiceImp implements VideoService {
    @Autowired
    private VideoMapper videoMapper;


    @Override
    public void checkVideoService(List<VideoDTO> videoDTOList) {
        for (VideoDTO v : videoDTOList) {
            videoMapper.checkVideo(v);
        }
    }

    @Override
    public PageResult pageQuery(VideoSearchDTO videoSearchDTO) {
        PageHelper.startPage(videoSearchDTO.getPage(), videoSearchDTO.getPageSize());
        Page<VideoVO> videos = videoMapper.pageQuery(videoSearchDTO);
        getVideoLikeAndCollect(videos.getResult());
        return new PageResult(videos.getTotal(), videos.getResult());
    }

    public void getVideoLikeAndCollect(List<VideoVO> list) {
        for (VideoVO v : list) {
            Long id = v.getId();
            Integer likeNum = videoMapper.getVideoByLikeId(id); // 获取视频点赞数
            Integer collectNum = videoMapper.getVideoByCollectId(id); // 获取视频收藏数
            v.setLikes(likeNum);
            v.setCollects(collectNum);
        }
    }
}
